#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

void Test1()
{

	string s1 = "gfedcba";
	sort(s1.begin(), s1.end());
	cout << s1;

}


int dx[4] = { 1,-1,0,0 };
int dy[4] = { 0,0,1,-1 };
int a, b;

    void dfs(int x, int y, vector<vector<int> >& m)
    {
        if (m[x][y] == 0)
        {
            return;
        }

        m[x][y] = 0;

        for (int k = 0; k < 4; k++)
        {
            int i = x + dx[k], j = y + dy[k];
            if (i >= 0 && i < a && j >= 0 && j < b)
            {
                dfs(i, j, m);
            }
        }
    }
    int citys(vector<vector<int> >& m) {
        int sum = 0;
        a = m.size();
        b = m[0].size();

        for (int i = 0; i < a; i++)
        {
            for (int j = 0; j < b; j++)
            {
                if (m[i][j] == 1)
                {
                    dfs(i, j, m);
                    sum++;
                }
            }
        }
        return sum;
    }

int main()
{

    vector<vector<int>> v = { {1,1,0},{1,1,0},{0,0,1} };

    cout << citys(v);

    return 0; 
}

class Solution {
public:
    bool vis[210] = { 0 };
    int n;
    int citys(vector<vector<int> >& m) {
        n = m.size();
        int sum = 0;
        for (int i = 0; i < n; i++) {
            if (!vis[i]) {
                sum++;
                dfs(m, i);
            }
        }

        return sum;
    }

    void dfs(vector<vector<int> >& m, int pos) {
        vis[pos] = true;

        for (int i = 0; i < n; i++) {
            if (!vis[i] && m[pos][i]) {
                dfs(m, i);
            }
        }
    }
};